package BOM.SELECTION;

import java.util.Vector;

import javax.swing.JTable;

import BOM.MASTER.m_master;
import BOM.MATERIAL.m_materials;
import TOOLS.SQLtool;

public class c_selection {
	
	protected SQLtool sql;
	
	public c_selection(SQLtool isql) throws Exception {
		sql=isql;
	}
	
	public static m_selection getSelection(SQLtool isql, m_master iMaster) throws Exception {
		m_selection result = new m_selection();
		result.master=iMaster;
		
		String str="SELECT Bs.BSIndex\r\n" + 
				"FROM [BOM].[BOMlist] pBl\r\n" + 
				"LEFT JOIN [BOM].[BOMselection] Bs ON Bs.BOMlistIndex=pBl.BOMlistIndex \r\n" + 
				"LEFT JOIN [dbo].[partMaster] pM ON pM.partIndex=Bs.materialIndex\r\n" + 
				"where pBl.partIndex="+iMaster.partIndex
				+"  and version='"+iMaster.version+"'  and Bs.BSIndex is not null \r\n" + 
				"GROUP BY Bs.BSIndex \r\n" + 
				"ORDER BY Bs.BSIndex ";
		JTable t=isql.select(str);
		if(t!=null && t.getRowCount()>0) {
			int len=t.getRowCount();
			result.binLen=len;
			result.bins=new m_selectionBIN[len];
			Thread [] IT=new Thread[len];
			for(int i=0;i<len;i++) {
				result.bins[i]=new m_selectionBIN();
				result.bins[i].binCode=Integer.parseInt(""+t.getValueAt(i, 0));
				IT[i]=getSelectionBIN(isql,iMaster,result,i);
			}
			for(int i=0;i<len;i++) {
				IT[i].join();
			}
		}
		
		return result;
	}

	
	
	private static Thread getSelectionBIN(SQLtool isql, m_master iMaster, m_selection bAba, int index) {
		Thread result=new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					String str="SELECT Bs.layer,Bs.name,Bs.materialIndex\r\n" + 
							",pM.partNumber as materialNumber,pM.partName as materialName  \r\n" + 
							"FROM [BOM].[BOMlist] pBl\r\n" + 
							"LEFT JOIN [BOM].[BOMselection] Bs ON Bs.BOMlistIndex=pBl.BOMlistIndex \r\n" + 
							"LEFT JOIN [dbo].[partMaster] pM ON pM.partIndex=Bs.materialIndex\r\n" + 
							"where pBl.partIndex=" + iMaster.partIndex 
							+"  and pBl.version='" + iMaster.version 
							+"' and Bs.BSIndex=" + bAba.bins[index].binCode + " \r\n" + 
							"ORDER BY Bs.layer,Bs.name,pM.partNumber "; 
					JTable t = isql.select(str);
					if (t != null && t.getRowCount() > 0) { 
						int len=t.getRowCount(); 
						bAba.bins[index].memberQty=len; 
						bAba.bins[index].members=new m_selectionMember[len]; 
						Vector<String> iTypeArray=new Vector<String>(); 
						Vector<m_materials> mTypeArray=new Vector<m_materials>(); 
						
						for(int i=0 ; i<len ; i++) { 
							String iLayer=""+t.getValueAt(i, 0);
							if(iLayer.length()<1) {
								iLayer="2";
							}
							String iName=""+t.getValueAt(i, 1);
							String iMaterialIndex=""+t.getValueAt(i, 2);
							String iMaterialNumber=""+t.getValueAt(i, 3);
							String iMaterialName=""+t.getValueAt(i, 4);
							String iTypeCounter=""+iMaterialIndex+iLayer;
							int flag=iTypeArray.indexOf(iTypeCounter);
							if(flag<0) {
								m_materials mTypeCount=new m_materials();
								mTypeCount.layer=Integer.parseInt(iLayer);
								mTypeCount.compNames=iName;
								mTypeCount.partIndex=Integer.parseInt(iMaterialIndex);
								mTypeCount.partNumber=iMaterialNumber;
								mTypeCount.partName=iMaterialName;
								mTypeCount.quantity=1;
								mTypeArray.add(mTypeCount);
								iTypeArray.add(iTypeCounter);
							}else {
								m_materials mTypeCount=mTypeArray.get(flag);
								mTypeCount.compNames=mTypeCount.compNames+","+iName;
								mTypeCount.quantity+=1;
							}
							bAba.bins[index].members[i]=new m_selectionMember();
							bAba.bins[index].members[i].layer=Integer.parseInt(iLayer);
							bAba.bins[index].members[i].compName=iName;
							bAba.bins[index].members[i].partIndex=Integer.parseInt(iMaterialIndex);
							bAba.bins[index].members[i].partNumber=iMaterialNumber;
							bAba.bins[index].members[i].partName=iMaterialName;
						} 
						bAba.bins[index].memberTypeQty=mTypeArray.size();
						Object [] counterArr=mTypeArray.toArray();
						bAba.bins[index].types=new m_materials[counterArr.length];
						for(int i=0;i<counterArr.length;i++) {
							bAba.bins[index].types[i]=(m_materials) counterArr[i];
						}
						iTypeArray.clear();
						mTypeArray.clear();
					}
				} catch (Exception e) {
					System.out.println(e.getMessage());;
				}
			}
		});
		result.start();
		return result;
	}

	public void close() {
		try {
			sql.close();
		} catch (Exception e) {
			;
		}
	}
}
